const express = require("express");
const { json, urlencoded } = require("body-parser");
const cors = require("cors");
const methodOverride = require('method-override')
const apiRouter = require("./router/api");
const usersRouter = require("./router/users");

const { users } = require("./data/index");
const userCtrl = require("./controllers/user");

const app = express();

// override with POST having ?_method=DELETE
app.use(methodOverride('_method'));

// public
app.use(express.static(__dirname + '/public'));

// body parser
app.use(json());
app.use(urlencoded({ extended: true }));

// cors 
app.use(cors());

// api router 
app.use('/api', apiRouter);
app.use('/users', usersRouter);

// view
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');

// view router
app.use("/", (req, res) => {
  res.render('index', { title: "Home" });
});

exports = module.exports = app;